package info.wangyuan.agent.service.conversation;

import info.wangyuan.agent.entity.dto.AgentChatResponseDTO;
import info.wangyuan.agent.entity.dto.ChatResponseDTO;
import org.springframework.stereotype.Component;

/**
 * 同步消息传输实现
 */
@Component
public class SyncMessageTransport implements MessageTransport<ChatResponseDTO> {

    @Override
    public ChatResponseDTO createConnection(long timeout) {
        return new ChatResponseDTO();
    }

    @Override
    public void sendMessage(ChatResponseDTO connection, AgentChatResponseDTO streamChatResponseDTO) {
        // 同步模式下，累积消息内容
        String existingContent = connection.getContent() != null ? connection.getContent() : "";
        connection.setContent(existingContent + streamChatResponseDTO.getContent());
        connection.setTimestamp(streamChatResponseDTO.getTimestamp());
    }

    @Override
    public void sendEndMessage(ChatResponseDTO connection, AgentChatResponseDTO streamChatResponseDTO) {
        // 同步模式下，设置最终响应
        if (streamChatResponseDTO.getContent() != null && !streamChatResponseDTO.getContent().isEmpty()) {
            String existingContent = connection.getContent() != null ? connection.getContent() : "";
            connection.setContent(existingContent + streamChatResponseDTO.getContent());
        }
        connection.setTimestamp(streamChatResponseDTO.getTimestamp());
    }

    @Override
    public void completeConnection(ChatResponseDTO connection) {
        // 同步模式下无需特殊处理
    }

    @Override
    public void handleError(ChatResponseDTO connection, Throwable error) {
        connection.setContent("错误: " + error.getMessage());
        connection.setTimestamp(System.currentTimeMillis());
    }
}
